/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-08-06
*/
package com.rzico.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Bom;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.BomItemService;
import com.rzico.basics.service.BomService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.Askfor;
import com.rzico.order.entity.GiftCard;
import com.rzico.order.entity.Order;
import com.rzico.order.service.GiftCardService;
import com.rzico.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 礼品卡控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-08-06
 */
@Api(description = "礼品卡接口")
@RestController
@RequestMapping("/giftCard")
public class GiftCardController extends BaseController {

    @Autowired
    private GiftCardService giftCardService;

    @Autowired
    protected MemberService memberService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private BomService bomService;

    @Autowired
    private BomItemService bomItemService;


    /**
     * 查询单条礼品卡
     *
     * @return
     */
    @ApiOperation("查询单条礼品卡")
    @GetMapping("/find/{id}")
    public CommResult<GiftCard> find(@PathVariable Long id) {
        GiftCard result = giftCardService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("giftCard",result);

        SysUser sysUser = sysUserService.selectByPrimaryKey(result.getMember().getUserId());
        if (sysUser!=null) {
            data.put("nickname",sysUser.getNickname());
            data.put("avatar",sysUser.getAvatar());
        }

        if (result.getType().equals(0)) {
            Map<String,Object> params = new HashMap<>();
            params.put("enterpriseId",result.getEnterpriseId());
            params.put("productId",result.getProductId());
            params.put("deleted",false);
            List<Bom> bomList = bomService.selectList(params);
            if (bomList.size()==0) {
               return CommResult.error("无商品清单");
            } else {
                data.put("itemList", bomItemService.getItemList(bomList.get(0).getId()));
            }

        } else {
            data.put("itemList", giftCardService.getItemList(id));
        }

        return CommResult.success(data);

    }

}
